<template>
  <a-drawer
    title="入学考试科目添加"
    width=450
    placement="right"
    :closable="false"
    @close="onClose"
    :visible="addVisibile"
    style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
    <a-form :form="form">
      <a-row :gutter="48">
        <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="院系">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="['ssyxm',{rules:[{required:true,message:'请选择院系'}]},{initialValue:''}]" @change='yxChange'>
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh+"-"+ i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="专业">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="['sszym',
                  {
                    rules: [{ required: true, message: '请选择专业!' }],
                  },
                  {initialValue:''}
                ]">
                <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zyh+"-"+i.zymc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="科目类型" >
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="['kmlxm',
                {
                  rules: [{ required: true, message: '请选择科目类型!' }],
                },
              {initialValue:''}]" @change="lxChange">
                <a-select-option v-for="i in selectMenu.kmlx" :key="i.code" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="科目编码" >
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="['kmbm',
              {
                rules: [{ required: true, message: '请选择科目编码!' }],
              },
              {initialValue:''}]" >
                <a-select-option v-for="i in selectMenu.kmbm" :key="i.id" :value="i.kmbm">{{ i.kmbm }}-{{i.kmmc}}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
      </a-row>
    </a-form>
    <div class="drawer-bootom-button">
      <a-button style="margin-right: .8rem" @click="onClose">取消</a-button>
      <a-button @click="handleSubmit" type="primary" :loading="loading">保存</a-button>
    </div>
  </a-drawer>
</template>

<script>
  // import moment from 'moment'
  import {mapState} from 'vuex'

  const formItemLayout = {
  labelCol: { span: 3 },
  wrapperCol: { span: 18 }
}

const selectArr = ['TYPE0126', 'TYPE0075', 'TYPE0125', 'TYPE0072']
export default {
  props: {
    addVisibile: {
      default: false
    }
  },
  data () {
    return {
      formItemLayout,
      loading: false,
      postParams: {
      },
      selectMenu: {
        yx: [] ,// 院系
        zy: [] ,//专业
        kmlx:[] ,//科目类型
        kmbm:[] //科目编码
      }
    }
  },
  beforeCreate() {
  	this.form = this.$form.createForm(this)
  },
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
      TYPE0126: state => state.app['TYPE0126'] // 年级
    })
  },
  created () {
    this.initSelectMenu()
    selectArr.forEach(v => {
      this.$store.dispatch('app/setDictionary', v)
    })
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        this.form.getFieldDecorator('ssyxm',{initialValue:res[0].data[0].dwh})
        this.getProssion(res[0].data[0].dwh)
        this.getkmbm(res[0].data[0].dwh)
        this.$api.base.dictionaryData('TYPE0430').then(res => {
            this.selectMenu.kmlx = res.data
        })
      })
    },
     yxChange(code){
      this.form.setFieldsValue({'sszym':''})

      this.$set(this.selectMenu, 'zy', [])
      // 获取专业
      if(code){
        this.getProssion(code)

      }
    },
    lxChange(code){
       this.form.setFieldsValue({'kmbm':''})
      this.$set(this.selectMenu,'kmbm',[])
      if(code){
          this.getkmbm(code)
      }
    },
    getProssion(code){
       this.$api.base.majorAll({
        lsdwh: code,zycc:'2'
      }).then(res => {
        this.$set(this.selectMenu, 'zy', res.data)
      })
    },
    getkmbm(code){
       // 获取科目编码
      this.$api.base.subjectCode({
        ssyxm:this.form.getFieldValue('ssyxm'),
        code:code
      }).then(res => {
        this.$set(this.selectMenu,'kmbm',res.data.rows)
      })
    },
    reset () {
      this.postParams = {}
    },
    // 关闭
    onClose () {
      this.reset()
      this.$emit('close')
      this.form.resetFields()
      this.initSelectMenu()
    },
    // 保存
    handleSubmit () {
      // this.reset()
      // this.$emit('success', this.postParams)
      this.form.validateFields( (err,values) => {
        if(err){
          this.$message.error("请选择必填项")
        }else{
          var kmmc ;
          this.selectMenu.kmbm.map((item,index) => {
            if(item.kmbm == this.form.getFieldValue('kmbm')){
              kmmc = item.kmmc
            }
          })
          var params = Object.assign({kmmc:kmmc},this.form.getFieldsValue())
          this.$api.recruitstat.entryResultManageAdd(params).then(res => {
            this.$emit('success')
            this.form.resetFields()
            this.initSelectMenu()
          }).finally(() => {
            this.loadingBtn = false
          })
        }
      })
    }
  }
}
</script>
